package com.qf.mybatis;

import com.qf.bean.Role;
import com.qf.bean.User;
import com.qf.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * projectName: MyBatis_1
 *
 * @author: MaTianWei
 * time: 2021/6/25 11:30
 * description:查询
 */

public class UseMybatisSelect {
    public static void main(String[] args) throws IOException {
        //1·读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
        //2·创建sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3·创建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(stream);
        //4·创建sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //5·获取mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

/*
        User query = mapper.query(1);
        System.out.println("query"+query);
*/

        List <User> users = mapper.queryRole();
        for(User user : users){
            System.out.println("-==-=-=-=-=--=--=-==-=-=-=-=-=-=-=-=-=-==-=--==-");
            System.out.println("用户信息");
            System.out.println("user"+user);
            System.out.println("用户的详细信息");
            System.out.println("user.getDetail"+user.getDetail());
            System.out.println("用户角色信息");
            List<Role> roles = user.getRoles();
            if (roles != null) {
                for (Role role : roles) {
                    System.out.println("role"+role);
                    System.out.println("角色对应的薪水");
                    System.out.println("role"+role.getSalary());
                }
            } else {
                System.out.println("用户没有角色");
            }
            System.out.println("-==-=-=-=-=--=--=-==-=-=-=-=-=-=-=-=-=-==-=--==-");
        }
        //6·关闭资源
        sqlSession.close();
    }
}
